package com.cmy.framework_base.app

import android.app.Application
import androidx.lifecycle.ProcessLifecycleOwner
import androidx.startup.AppInitializer
import com.cmy.framework_base.activity.ActivityContext
import com.cmy.framework_base.helper.AppFrontBackHelper
import com.cmy.framework_utils.log.KLog

open class BaseApp: Application() {
    private var isDarkModel = true

    override fun onCreate() {
        super.onCreate()
        setApp(this)
        attemptToInitKits()
        registerObserver()
        AppFrontBackHelper.register(this,object : AppFrontBackHelper.OnAppStatusListener{
            override fun onBack() {
                KLog.i("TAG","进入后台运行")
            }

            override fun onFront() {
                KLog.i("TAG","进入前台运行")
            }
        })
    }

    fun isDarkModel(): Boolean {
        return isDarkModel
    }

    companion object {
        lateinit var instance: BaseApp
            @JvmStatic get
            private set

        @JvmStatic
        fun setApp(app: BaseApp) {
            instance = app
        }


    }


    /**
     * 尝试初始化三方SDK
     */
   private fun attemptToInitKits() {
            AppInitializer.getInstance(instance)
                .initializeComponent(LazyInitializer::class.java)

    }


    /**
     * 添加
     */
    private fun  registerObserver(){
        ProcessLifecycleOwner.get().lifecycle.addObserver(ActivityContext.getInstance())
        registerActivityLifecycleCallbacks(ActivityContext.getInstance())
    }

}